package com.nativetalk.controller.app.index;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.banner.TdNtBannerInfoIndex;
import com.nativetalk.bean.category.TsNtCategoryIndex;
import com.nativetalk.bean.course.TsNtCourseInfoIndex;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class IndexController {
    private static Logger logger = Logger.getLogger(IndexController.class);

    /**
     * @return RetInfo
     * @Purpose 获取banner图信息 及子菜单信息  父菜单
     * @version 1.0
     * @time 2015-12-25
     * @author lizhun
     */
    @RequestMapping(value = "/app/1/index", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo list() {
        try {
            //打印入参
            logger.error(Constants.prefix_out);

            RetInfo retInfo = new RetInfo();
            Map<String, Object> map = new HashMap<>();
            List<TdNtBannerInfoIndex> tdNtBannerInfoIndices = (List<TdNtBannerInfoIndex>) MemcachedUtils.get(MemcachedKey.bannerInfoAPPIndexs);
            map.put("banner", tdNtBannerInfoIndices);
            List<TsNtCategoryIndex> tsNtCategoryIndexParent = (List<TsNtCategoryIndex>) MemcachedUtils.get(MemcachedKey.tsNtCategoryIndexParent);
            map.put("categoryParent", tsNtCategoryIndexParent);
            Map<String, List<TsNtCategoryIndex>> tsNtCategoryIndexsMap = (Map<String, List<TsNtCategoryIndex>>) MemcachedUtils.get(MemcachedKey.tsNtCategoryIndexsMap);
            map.put("category", tsNtCategoryIndexsMap.get(tsNtCategoryIndexParent.get(0).getCate_id().toString()));
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取首页数据成功");
            retInfo.setObj(map);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 获取子菜单信息
     * @version 1.0
     * @time 2015-12-25
     * @author lizhun
     */
    @RequestMapping(value = "/app/1/index/category", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo category(@RequestBody Map<String, String> mapParam) {
        try {
            //打印入参
            logger.error(Constants.prefix_out + JsonUtil.toJson(mapParam));

            RetInfo retInfo = new RetInfo();
            Map<String, Object> map = new HashMap<>();
            Map<String, List<TsNtCategoryIndex>> tsNtCategoryIndexsMap = (Map<String, List<TsNtCategoryIndex>>) MemcachedUtils.get(MemcachedKey.tsNtCategoryIndexsMap);
            map.put("category", tsNtCategoryIndexsMap.get(mapParam.get("cate_id")));
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取首页子菜单成功");
            retInfo.setObj(map);
            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 获取子菜单下教材信息
     * @version 1.0
     * @time 2015-12-25
     * @author lizhun
     */
    @RequestMapping(value = "/app/1/index/course", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo course(@RequestBody Map<String, String> mapParam) {
        try {
            //打印入参
            logger.error(Constants.prefix_out + JsonUtil.toJson(mapParam));

            RetInfo retInfo = new RetInfo();
            Map<String, Object> map = new HashMap<>();
            Map<String, List<TsNtCourseInfoIndex>> tsNtCourseInfoIndexMap = (Map<String, List<TsNtCourseInfoIndex>>) MemcachedUtils.get(MemcachedKey.tsNtCourseInfoIndexMap);
            map.put("courseList", tsNtCourseInfoIndexMap.get(mapParam.get("cate_id")));
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取首页子菜单下教材成功");
            retInfo.setObj(map);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
}


